using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NonIntrusive_Debugger;

namespace PasswordGrabber
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("Usage: Stealer.exe <<PathToGSBinary>>");
                return;
            }
            Debugger debug = new Debugger();
            debug.Execute(args[0], "", false);
            Console.WriteLine("Correcting VirtualProtect");
            int vp = debug.getFunctionAddress("VirtualProtect", "kernel32.dll");

            debug.setBP(vp);
            debug.waitForBP(vp);

            while (!(debug.getDWORDValue(debug.getRegister(Debugger.REGISTERS.ESP) + 12) == 1))
            {
                debug.resetBP(vp, true);
                debug.waitForBP(vp);
            }
            debug.clearBP(vp);
            debug.setDWORDValue(debug.getRegister(Debugger.REGISTERS.ESP) + 12, 0x80);

            Console.WriteLine("Waiting for Breakpoints");
            for (int x = 0; x < 20000; x++)
            {
                int value= debug.waitForBP(15);
                if (value != -1) { break; }
                int[] results = null;
                /* Haval Update (License Pass) */
                debug.setSearchString("9090535657558BF18BFA8BD8807B??007516");
                debug.setSearchStyle(Debugger.SEARCH_STYLES.FindAll);
                results = debug.searchImage();
                if (results.Length > 0)
                {
                    foreach (int n in results)
                    {
                        debug.setBP(n + 2);
                    }
                }
                debug.setSearchString("909090558BEC83C4D48955FC8945D4B8????????E8");
                debug.setSearchStyle(Debugger.SEARCH_STYLES.FindAll);
                results = debug.searchImage();
                if (results.Length > 0)
                {
                    foreach (int n in results)
                    {
                        debug.setBP(n + 3);
                    }
                }
            }
            int bpsLeft = 2;

            while (bpsLeft > 0 && (!debug.processHasEnded()))
            {
                int x = debug.waitForBP();
                if (x > -1)
                {
                    debug.clearBP(debug.getRegister(Debugger.REGISTERS.EIP));
                    Console.WriteLine("Password " + Math.Abs(( bpsLeft - 3)) + ": " + debug.getASCII(debug.getRegister(Debugger.REGISTERS.EDX), 255));
                    bpsLeft--;
                }
            }
            Console.WriteLine("Passwords Located\nPress any Key to Exit...");
            debug.Resume();
            debug.Terminate();
            Console.ReadKey();
        }
    }
}
